package com.haojiang.mh.remote.adapter


import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.haojiang.mh.AppContext
import com.haojiang.mh.R
import com.haojiang.mh.remote.bean.AlarmBean
import com.haojiang.mh.remote.bean.ControlLeftMenuBean
import com.haojiang.mh.remote.utils.BleUtil.Companion.isDevConncted
import com.haojiang.mh.remote.utils.BleUtil.Companion.lastConnect
import com.haojiang.mh.remote.utils.SpUtil
import com.haojiang.mh.remote.utils.Util.Companion.playSound
import com.haojiang.mh.remote.widget.AlarmItemNewView
import kotlinx.android.synthetic.main.item_alarm.view.*


class AlarmNewFragAdapter : RecyclerView.Adapter<AlarmNewFragAdapter.ViewHolder>() {

    private val SECTION_ALARM_NEW_MEM: String = "section_alarm_new_mem"
    private val context = AppContext.instance()
    private val dataList: MutableList<AlarmBean> = mutableListOf() //视图类型列表--功能代码
    private val viewList: MutableList<View> = mutableListOf() //视图列表
    private val selectCodeList = ArrayList<String>()    //选择代码列表


    @Synchronized
    fun setData(alarmList: ArrayList<String>) {
        if(lastConnect.bleDeviceList.size > 0) {
            val codeLIst = SpUtil.getListData(context, SECTION_ALARM_NEW_MEM, lastConnect.bleDeviceList.get(0).bleAddress, String::class.java)
            selectCodeList.clear()
            selectCodeList.addAll(codeLIst)
            viewList.clear()
            dataList.clear()
//        alarmList.forEach {
//            if (!it.equals(CmdKey.MSG_HEAD_INTENSITY_INC) &&
//                !it.equals(CmdKey.MSG_FOOT_INTENSITY_INC)
//            ) {
//                dataList.add(ByteUtils.hexToInt(it))
//            }
//        }
            notifyDataSetChanged()
        }
    }


    /**
     * 获取选中的记忆位置列表
     */
    fun getMemList(): ArrayList<String> {
        val memList = ArrayList<String>()
        viewList.forEach {
            if (it.isSelected) {
                memList.add(it.rmText.code!!)
            }
        }
        if (isDevConncted()) {
            if (memList.size > 0) {
                lastConnect.bleDeviceList.forEach {
                    SpUtil.putListData(context, SECTION_ALARM_NEW_MEM, it.bleAddress, memList)
                }
            } else {
                lastConnect.bleDeviceList.forEach {
                    SpUtil.deleteSectionKey(context, SECTION_ALARM_NEW_MEM, it.bleAddress)
                }
            }
        }
        return memList
    }


    override fun getItemCount(): Int {
        return 3
    }

    /**
     * 设置界面
     */
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        if (!viewList.contains(holder.itemView)) {
            viewList.add(holder.itemView)

            for (i in 0 until selectCodeList.size) {
                if (selectCodeList[i].equals(holder.itemView.rmText.code, ignoreCase = true)) {
                    holder.itemView.isSelected = true
//                    holder.itemView.ivSelector.setImageResource(R.drawable.ic_select)
                    holder.itemView.switchCheck.isChecked = true
                    break
                } else {
                    holder.itemView.isSelected = false
//                    holder.itemView.ivSelector.setImageResource(R.drawable.ic_unselect)
                    holder.itemView.switchCheck.isChecked = false
                }
            }

            holder.itemView.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    playSound()
                    viewList.forEach {
                        if (it.equals(v!!)) {
                            it.isSelected = !it.isSelected
                            if (it.isSelected) {
//                                v.ivSelector.setImageResource(R.drawable.ic_select)
                                v.switchCheck.isChecked = true
                            } else {
//                                it.ivSelector.setImageResource(R.drawable.ic_unselect)
                                v.switchCheck.isChecked = false
                            }
                        } else {
                            it.isSelected = false
//                            it.ivSelector.setImageResource(R.drawable.ic_unselect)
                            it.switchCheck.isChecked = false
                        }
                    }
                }

            })
        }

    }


//    override fun getItemViewType(position: Int): Int {
//        return dataList.get(position)
//    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val viewHolder = ViewHolder(AlarmItemNewView(parent.context))
//        viewHolder.itemView.rmText.code = ByteUtils.intToHex(viewType)
        return viewHolder
    }


    //设置选中功能
//    fun setSelectData(codeList: ArrayList<String>) {
//        this.selectCodeList.clear()
//        this.selectCodeList.addAll(codeList)
//    }


//    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
//        when (viewType) {
//            ByteUtils.hexToInt(CmdKey.TV_BACK_POS!!) -> {
//                val viewHolder = ViewHolder(
//                    AlarmTvItemView(parent.context)
//                )
//                return viewHolder
//            }
//
//            ByteUtils.hexToInt(CmdKey.ZG_BACK_POS!!) -> {
//                val viewHolder = ViewHolder(
//                    AlarmZgItemView(parent.context)
//                )
//                return viewHolder
//            }
//
//            ByteUtils.hexToInt(CmdKey.SNORE_BACK_POS!!) -> {
//                val viewHolder = ViewHolder(
//                    AlarmSnoreItemView(parent.context)
//                )
//                return viewHolder
//            }
//
//            ByteUtils.hexToInt(CmdKey.LOUNGE_BACK_POS!!) -> {
//                val viewHolder = ViewHolder(
//                    AlarmLoungeItemView(parent.context)
//                )
//                return viewHolder
//            }
//
//            ByteUtils.hexToInt(CmdKey.M1_BACK_POS!!) -> {
//                val viewHolder = ViewHolder(
//                    AlarmM1ItemView(parent.context)
//                )
//                return viewHolder
//            }
//
//            ByteUtils.hexToInt(CmdKey.M2_BACK_POS!!) -> {
//                val viewHolder = ViewHolder(
//                    AlarmM2ItemView(parent.context)
//                )
//                return viewHolder
//            }
//
//            ByteUtils.hexToInt(CmdKey.M3_BACK_POS!!) -> {
//                val viewHolder = ViewHolder(
//                    AlarmM3ItemView(parent.context)
//                )
//                return viewHolder
//            }
//            else -> {
//                val viewHolder = ViewHolder(
//                    AlarmTvItemView(parent.context)
//                )
//                return viewHolder
//            }
//        }
//    }
//
//
//    fun setSelectData(code: String) {
//        viewList.forEach {
//            if (it.rmText.code.equals(code, ignoreCase = true)) {
//                it.ivSelector.setImageResource(R.drawable.ic_select)
//            }
//        }
//    }


    /**
     * adapter的ViewHolder类
     */
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {}


    interface IAlarmtemClickListener {
        fun onItemClick(
            controlLeftMenuItem: ControlLeftMenuBean
        )
    }
}
